package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.CollectDao;
import com.gdit.zax.entitys.Collect;
import com.gdit.zax.entitys.Course;
import com.gdit.zax.entitys.CourseCollect;
import com.gdit.zax.entitys.VideoInfo;
import com.gdit.zax.utils.DBUtil;


public class CollectDaoImpl implements CollectDao{
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public CollectDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}


	public boolean addCourseCollect(int uid, int courseid) {
		try {
			String sql = "insert t_collect set UserId=?,CourseId=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, courseid);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	public boolean addNoteCollect(int uid, int nid) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean addSectionCollect(int uid, int sid) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean addVideoCollect(int uid, VideoInfo video) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean delCourseCollect(int collectid) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean delNoteCollect(int cid) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean delSectionCollect(int cid) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean delVideoCollect(int cid) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean isCourseCollect(int uid, int cid) {
		String sql = "select * from t_collect where UserId=? and CourseId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, cid);
			
			rs = ps.executeQuery();
			
			if (rs.next()){
				return true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	public boolean isNoteCollect(int uid, int cid) {
		
		return false;
	}


	public boolean isSectionCollect(int uid, int cid) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean isVideoCollect(int uid, int cid) {
		// TODO Auto-generated method stub
		return false;
	}


	public ArrayList<Collect> listCourseCollect(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public ArrayList<CourseCollect> listCourseCollect(int uid, int page,int onepagecount) {
		ArrayList<CourseCollect> list = new ArrayList<CourseCollect>();
//		String sql = "select * from t_collect where UserId=? limit ?,?";
		String sql = "select *,TO_DAYS(now())-TO_DAYS(c.AddTime)<7 as IsNew from t_collect as c inner join t_course as co on c.CourseId=co.Id inner join t_user as u on co.AnthorId=u.Id where UserId=? limit ?,? ";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				//http://pingkgs3pc:8080/zuiaixue/getcollectlist?accesstoken=1765cac7fc56625ec2605afc299d1171
				CourseCollect be = DBUtil.getJoinObject(rs, CourseCollect.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<Collect> listNoteCollect(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public ArrayList<Collect> listNoteCollect(int uid, int page,
			int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public ArrayList<Collect> listSectionCollect(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public ArrayList<Collect> listSectionCollect(int uid, int page,int onepagecount) {
		ArrayList<Collect> list = new ArrayList<Collect>();
		String sql = "select * from t_collect where UserId=? limit ?,?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Collect be = DBUtil.getobject(rs, Collect.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<Collect> listVideoCollect(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public ArrayList<Collect> listVideoCollect(int uid, int page,int onepagecount) {
		ArrayList<Collect> list = new ArrayList<Collect>();
		String sql = "select * from t_collect where UserId=? limit ?,?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Collect be = DBUtil.getobject(rs, Collect.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public int getCourseCollectSum(int uid) {
		String sql = "select COUNT(distinct CourseId) from t_collect  WHERE UserId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}


	public int getNoteCollectSum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}


	public int getSectionCollectSum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}


	public int getVideoCollectSum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	
}
